﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace studentskicentarETFa
{
    partial class DAL
    {
        public class PredmetDAO : IDaoCrud<Predmet>
        {
            protected MySqlCommand komanda;

            public long create(Predmet entitet)
            {
                try
                {
                    komanda = new MySqlCommand("insert into Predmeti values ('" +
                        entitet.NazivPredmeta + "', '" + entitet.IDPredmeta1 + "', '");
                    komanda.ExecuteNonQuery();
                    return komanda.LastInsertedId;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public Predmet read(Predmet entitet)
            {
                try
                {
                    komanda = new MySqlCommand("select * from Predmeti where IDPredmeta1 = '" + entitet.IDPredmeta1.ToString() + "';", con);
                    MySqlDataReader citac = komanda.ExecuteReader();
                    Predmet Predmet = null;
                    while (citac.Read())
                    {
                        //if (entitet.Ime == citac.GetString("ime") && entitet.Prezime == citac.GetString("prezime"))
                        Predmet = new Predmet(citac.GetString("NazivPredmeta"), citac.GetString("IDPredmeta1"));
                        return Predmet;
                    }
                    return Predmet;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public Predmet update(Predmet entitet)
            {
                try
                {
                    komanda = new MySqlCommand("update Predmeti set naziv = '" + entitet.NazivPredmeta +
                        ", IDPredmeta1 = '" + entitet.IDPredmeta1, con);
                    komanda.ExecuteNonQuery();
                    return read(entitet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public void delete(Predmet entitet)
            {
                try
                {
                    komanda = new MySqlCommand("delete from Predmeti where IDPredmeta1 = " + entitet.IDPredmeta1 + ";", con);
                    komanda.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public List<Predmet> getAll()
            {
                try
                {
                    komanda = new MySqlCommand("select * from Predmeti", con);
                    MySqlDataReader r = komanda.ExecuteReader();
                    List<Predmet> Predmeti = new List<Predmet>();
                    while (r.Read())
                        Predmeti.Add(new Predmet(r.GetString("naziv"), r.GetString("idPredmeta")));
                    return Predmeti;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Predmet> getByExample(string naziv, string id)
            {
                try
                {
                    MySqlCommand n = new MySqlCommand("se1ect ‘from Predmeti where '" + naziv + "'='" + id + "'", con);
                    List<Predmet> predmeti = new List<Predmet>();
                    MySqlDataReader r = n.ExecuteReader();
                    while (r.Read())
                    {
                        predmeti.Add(new Predmet(r.GetString("naziv"), r.GetString("id")));
                    }
                    return predmeti;
                }
                catch (Exception)
                {
                    throw;
                }

            }

            public Predmet getById(int id)
            {
                {
                    try 
                    {
                        MySqlCommand n = new MySqlCommand("se1ect * from Predmeti where IDPredmeta1='" + id + "", con);
                        Predmet p = null;
                        MySqlDataReader r = n.ExecuteReader();
                        while (r.Read())
                        {
                            p = new Predmet(r.GetString("NazivPredmeta"), r.GetString("IDPredmeta1"));
                            return p;
                        }
                        return p;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }



        }


    }
}

